<?php
/************************************
*   熊店
*   @file:/server/bearshop/__include/data_model/mod_user.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Fri Feb 12 10:27:58 CST 2010
************************************/

class mod_product_buy extends data_model 
{
    
    public function __construct($db_config)
    {
        parent::__construct($db_config);
        $this->_table = 'tl_product_buy';
    }

    public function create($uid , $pid)
    {
        $this->start(K5_DATAMOD_NOHASH);
        
        $this->set('pid' , $pid);
        $this->set('uid' , $uid);
        $this->set('stat' , K_COMMON_NO);
        $this->set('cip' , K::get_ip());
        $this->set('ctime' , time());
        
        return $this->insert();
    }
    
    public function get_by_uid_pid($uid , $pid)
    {
        $this->start();
        $this->set_condition('uid='.$uid.' AND pid='.$pid);
        return $this->fetch();
    }
    
    public function is_exist_uid_pid($uid , $pid , &$is_exist)
    {
        $this->start();
        $this->set_condition('uid='.$uid.' AND pid='.$pid);
        $rs = $this->fetch_count($n);
        if(!$rs)
            return false;
        $is_exist = $n > 0 ? true : false;
        return true;
    }
    
    public function ls($stat , &$total , $pagesize =10 , $page=1)
    {
        $this->start();
        $this->set_condition('stat='.$stat);
        $this->fetch_count($total);
        return $this->fetch_multi();
    }
    
    public function list_uid($aStat , &$total , $page = 1 , $pagesize = 10)
    {
        $this->start();
        $start = $this->get_limit_start($page , $pagesize);
//        $sql = 'SELECT distinct(uid),count(*) n,max(ctime) ctime  FROM '.K5_DATAMOD_TB.' WHERE stat in ('.implode(',' , $aStat).') ORDER BY ctime LIMIT '.$start.' , '.$pagesize;
        $sql = 'SELECT uid,max(ctime) ctime,count(*) n FROM `'.K5_DATAMOD_TB.'` WHERE stat in ('.implode(',' , $aStat).') group by uid ORDER BY ctime  LIMIT '.$start.' , '.$pagesize;
        $this->fetch_count($total);
        $rs = $this->fetch_by_sql($sql , true);
        if(!$rs)
            return false;
        if(count($this->_data) == 1 && empty($this->_data[0]['uid']))
            $this->_data = array();
        
        return true;
    }
    
    public function list_pid($aStat , &$total , $page = 1 , $pagesize = 10)
    {
        $this->start();
        $start = $this->get_limit_start($page , $pagesize);
        $sql = 'SELECT distinct(pid) pid FROM '.K5_DATAMOD_TB.' WHERE stat in ('.implode(',' , $aStat).') ORDER BY ctime LIMIT '.$start.' , '.$pagesize;
        
        $this->fetch_count($total);
        return $this->fetch_by_sql($sql , true);
    }
    
    public function list_by_pid($aPid , $aStat , $pagesize = 10 , $page = 1)
    {
        $this->start();
        $this->set_condition('pid in ('.implode(',' , $aPid).') AND stat in ('.implode(',' , $aStat).') ');
        return $this->fetch_multi();
    }
    
    public function list_by_uid($aUid , $aStat , $pagesize = 10 , $page = 1)
    {
        $this->start();
        $this->set_condition('uid in ('.implode(',' , $aUid).') AND stat in ('.implode(',' , $aStat).') ');
        $start = ($page-1) * $pagesize;
        return $this->fetch_multi('' , '' , $start , $pagesize);
    }
    
    public function check_by_uid_pid($uid , $aPid)
    {
        $this->start();
        $this->set_condition('uid = '.$uid.' AND pid in ('.implode(',' , $aPid).') ');
        $rs = $this->fetch_multi('');
        if(!$rs)
        	return false;
        $this->_data = $this->get_key2map('pid' , 'pid');
        return true;
    }
    
    
    public function list_by_id($aId)
    {
        $this->start();
        $this->set_condition('id in ('.implode(',' , $aId).')');
        return $this->fetch_multi();
    }
    
    public function get_by_id($id)
    {
        $this->start();
        $this->set_condition('id = '.$id);
        return $this->fetch();
    }
    
    public function quote_price($id , $price)
    {
        $this->start();
        $this->set_condition('id='.$id);
        $this->set('quoted_price' , $price);
        $this->set('stat' , K_COMMON_YES);
        return $this->update();
    }
    
    public function lock_by_pid($pid)
    {
        $this->start();
        $this->set_condition('pid = '.$pid);
        $this->set('stat' , K_COMMON_LOCK);
        return $this->update();
    }
    
    public function done_order($id , $price , $stat_done = true , $buy_num = 1)
    {
        $this->start();
        $this->set_condition('id = '.$id);
        if($stat_done)
        	$this->set('stat' , K_COMMON_DONE);
        $this->set('done_price' , $price);
        $this->set('buy_num' , $buy_num);
        $this->set('done_time' , time());
        return $this->update();
    }
    
    public function cancel_order($id)
    {
        $this->start();
        $this->set_condition('id='.$id);
        return $this->delete(1);
    }
}
?>